import {show} from "../../service/goods";
import {collect} from "../../service/goods";
import {AddCart} from "../../service/cart";
// pages/goods/goods.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_id:null,
    goods:{},
    comments:[],
    like_goods:[],
    is_collect:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    const {id} = options
      show(id).then(res=>{
        this.setData({
            is_collect:res.goods.is_collect,
            goods_id:id,
            goods:res.goods,
            comments:res.goods.comments,
            like_goods:res.like_goods
          })
      })
  },


  /*
  *收藏和取消收藏
  */
  onCollect(){
    collect(this.data.goods_id).then(res=>{
      console.log(res);
      this.setData({
        is_collect:this.data.is_collect == 0 ? 1 : 0
      })
        wx.showToast({
          title:this.data.is_collect == 0 ? '已取消收藏' : '收藏成功',
          icon:'success',
          duration:2000
        })
    })
  },

  /*
  * 加入购物车
  */
  onAddCart(){
    AddCart(this.data.goods_id).then(res=>{
      wx.showToast({
        title:'成功加入购物车',
        icon:'success',
        duration:2000
      })
    })
  }


})